/**
 * 	 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *   @author Colin Redmond
 *   Copyright (C) 2011 Colin Redmond
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package com.cred.industries.platform.business.facade {
	
	import com.cred.industries.platform.dao.MetricsServerDAO;
	import com.cred.industries.platform.model.LocalUserData;
	
	import flash.events.EventDispatcher;

	/**
	 * Singleton instanace that holds all data related to metrics
	 * Also responcible for accessing the data layer to update
	 * the server.
	 */
	public class MetricsFacade extends EventDispatcher {
		private static var sInstance:MetricsFacade = new MetricsFacade();
		
		//metrics data we are collecting
		private var mAppStartTime:Number = 0;
		
		public function MetricsFacade() { }
		
		public static function get():MetricsFacade {
			
			return sInstance;
		}
		
		/**
		 * Call when starting the app. So we can track
		 * how long the users are in the app
		 */
		public function appStarted():void {
			
			mAppStartTime = new Date().time;
		}
		
		/**
		 * Call when we log out or close the app. 
		 * Will update the server with the information.
		 * This is fire and forget so no listeners needed
		 */
		public function appClosed():void {
			
			if(LocalUserData.get().isLoggedIn()) {
				
				var timeInApp:Number = new Date().time - mAppStartTime;
				
				var metricsDAO:MetricsServerDAO = new MetricsServerDAO();
				metricsDAO.sendTimeInApp(timeInApp);
			}
		}
	}
}